package com.singlemountain.gcommerce.web.common;


import com.singlemountain.gcommerce.domain.image.Imaged;
import com.singlemountain.gcommerce.domain.user.Userd;
import com.singlemountain.gcommerce.utils.TransferImageUtils;
import com.singlemountain.gcommerce.web.BaseController;
import com.singlemountain.gcommerce.web.BaseValidators;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class ImageController extends BaseController<Userd, BaseValidators> {
    public final String URL_IMAGE = "/image/{id}";
    public final String URL_IMAGE_DELETE = "/imageDelete/{id}";
    public final String URL_COMMON_IMAGE = "/commonImage";
    public final String VIEW_COMMON_IMAGE = "view_commonImage";


    @RequestMapping(value = URL_IMAGE, method = RequestMethod.GET)
    public void getImage(@PathVariable(value = "id") Long id, HttpServletResponse response) throws IOException {
        //System.out.println(id);
        Imaged imaged = Imaged.findImaged(id);
        response.setContentType("image/jpeg");
        ServletOutputStream out = response.getOutputStream();
        //System.out.println(imaged.getId());
        out.write(imaged.getBig());
        out.flush();
        out.close();

    }

    @RequestMapping(value = URL_IMAGE, method = RequestMethod.DELETE)
    public void deleteImage(@PathVariable(value = "id") Long id, HttpServletResponse response) {
        Imaged imaged = Imaged.findImaged(id);
        imaged.remove();

    }

    @RequestMapping(value = URL_COMMON_IMAGE, method = RequestMethod.GET)
    public String setUpPage() {
        System.out.println("wwwwwwwwwwwwww");
        return VIEW_COMMON_IMAGE;
    }
/*
    [
    {
        "name":"picture1.jpg",
            "size":902604,
            "url":"\/\/example.org\/files\/picture1.jpg",
            "thumbnail_url":"\/\/example.org\/thumbnails\/picture1.jpg",
            "delete_url":"\/\/example.org\/upload-handler?file=picture1.jpg",
            "delete_type":"DELETE"
    },
    {
        "name":"picture2.jpg",
            "size":841946,
            "url":"\/\/example.org\/files\/picture2.jpg",
            "thumbnail_url":"\/\/example.org\/thumbnails\/picture2.jpg",
            "delete_url":"\/\/example.org\/upload-handler?file=picture2.jpg",
            "delete_type":"DELETE"
    }
    ]*/


    @ResponseBody
    @RequestMapping(value = URL_COMMON_IMAGE, method = RequestMethod.POST)
    public String handle(@RequestParam("imageFile") CommonsMultipartFile mFile, HttpServletRequest httpRequest) {
        System.out.println(mFile.getOriginalFilename());

        Imaged imaged = new Imaged();
        imaged.setName(mFile.getOriginalFilename());
        imaged.setBig(mFile.getBytes());
        imaged.persist();
        System.out.println("3333333333");


        return TransferImageUtils.imageDomain2JsonListString(imaged);
    }

    @ModelAttribute
    public Imaged setModel() {
        return new Imaged();
    }
}
